rm(list=ls())
# Create folder to receive data
dir.create(here::here("data","raw","IBGE"))

# Date of download
date <- Sys.Date()

# Download table 5938 with gdp mun from 2002 onwards (Municipal GDP) - CN ref 2010
tab5938_url <-"https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela5938.xlsx&terr=NC&rank=-&query=t/5938/g/87/v/37/p/all/d/v37%200/l/v,p,t"
download.file(tab5938_url, here::here("data","raw","IBGE",paste0("tab5938_gdp_mun_downloaded_",date,".xlsx")))

# Download table 21 with gdp mun from 1999 to 2001 (Municipal GDP) - CN ref 2002
tab21_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela21.xlsx&terr=NC&rank=-&query=t/21/g/87/v/37/p/first%203/d/v37%200/l/v,p,t"
download.file(tab21_url, here::here("data","raw","IBGE",paste0("tab21_gdp_mun_downloaded_",date,".xlsx")))

# Download table 6784 with national accounts data (gdp deflator)
tab6784_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela6784.xlsx&terr=N&rank=-&query=t/6784/n1/all/v/all/p/all/d/v9810%201,v9811%201,v9812%202,v9813%202,v9814%201/l/v,,t%2Bp"
download.file(tab6784_url, here::here("data","raw","IBGE",paste0("tab6784_national_accounts_downloaded_",date,".xlsx")))

# Download table 616 with economic active population (labor force)
tab616_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela616.xlsx&terr=NC&rank=-&query=t/616/n6/all/v/1000140/p/all/c90/3287/c2/0/c1/0/c58/0/d/v1000140%202/l/p,,t%2Bc1%2Bv%2Bc58%2Bc90%2Bc2"
download.file(tab616_url, here::here("data","raw","IBGE",paste0("tab616_pea_downloaded_",date,".xlsx")))

# Download tables 6579, 793 and 202 with mun population
tab6579_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela6579.xlsx&terr=NC&rank=-&query=t/6579/n6/all/v/all/p/all/l/p,,t%2Bv"
download.file(tab6579_url, here::here("data","raw","IBGE",paste("tab6579_mun_population_downloaded_",date,".xlsx")))

tab793_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela793.xlsx&terr=NC&rank=-&query=t/793/n6/all/v/all/p/all/l/v,,t%2Bp"
download.file(tab793_url, here::here("data","raw","IBGE", paste0("tab793_mun_population_downloaded_",date,".xlsx")))

tab202_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela202.xlsx&terr=NC&rank=-&query=t/202/n6/all/u/y/v/allxp/p/all/c2/0/c1/0/l/p,,t%2Bv%2Bc2%2Bc1"
download.file(tab202_url, here::here("data","raw","IBGE", paste0("tab202_mun_population_downloaded_",date,".xlsx")))

# Download table 136 with mun population by race 
tab136_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela136.xlsx&terr=NC&rank=-&query=t/136/n6/all/v/1000093/p/all/c86/2776/d/v1000093%202/l/p,,t%2Bv%2Bc86"
download.file(tab136_url, here::here("data","raw","IBGE", paste0("tab136_mun_population_by_race_downloaded_",date,".xlsx")))

# Download table 1309 with mun by urban vs rural
tab1309_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela1309.xlsx&terr=NC&rank=-&query=t/1309/n6/all/v/1000093/p/all/c2/0/c11277/90749,90750,90751/d/v1000093%202/l/p%2Bc11277,,t%2Bv%2Bc2"
download.file(tab1309_url, here::here("data","raw","IBGE", paste0("tab1309_mun_urban_vs_rural_downloaded_",date,".xlsx")))

# Download table 200 with mun by pop by age
tab200_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela200.xlsx&terr=NC&rank=-&query=t/200/n6/all/u/y/v/allxp/p/last%203/c2/0/c1/0/c58/0,1140,1141,1142,2498/d/v93%200/l/p%2Bc58,,t%2Bc2%2Bv%2Bc1"
download.file(tab200_url, here::here("data","raw","IBGE", paste0("tab200_mun_population_by_age_downloaded_",date,".xlsx")))

# Download correspondence table between municipalities and microrregions
tab_mun_microregion_url <- "https://sidra.ibge.gov.br/Territorio/DownloadUnidadesAbrangidas?formato=xlsx&nivel=6&by_nivel=9"
download.file(tab_mun_microregion_url, here::here("data","raw","IBGE", paste0("tab_mun_microregion_downloaded_",date,".xlsx")))

# Download percentage of residents at least 10 years old that are literate
tab1521_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela1521.xlsx&terr=NC&rank=-&query=t/1521/n6/all/v/1000676/p/all/c1/0/c2/0/c59/allxt/c11577/0/d/v1000676%202/l/v,p%2Bc1%2Bc2,t%2Bc59%2Bc11577"
download.file(tab1521_url, here::here("data","raw","IBGE", paste0("tab1521_mun_literacy_2000_downloaded_",date,".xlsx")))

tab1383_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela1383.xlsx&terr=NC&rank=-&query=t/1383/n6/all/v/all/p/all/c2/6794/d/v1646%201/l/v,p%2Bc2,t"
download.file(tab1383_url, here::here("data","raw","IBGE", paste0("tab1383_mun_literacy_2010_downloaded_",date,".xlsx")))

# Download percentage of residents at least 10 years old that can read
tab2982_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela2982.xlsx&terr=NC&rank=-&query=t/2982/n6/all/v/1001643/p/all/c12400/108886,108887/c86/0/c58/0/d/v1001643%202/l/v,p%2Bc12400%2Bc86,t%2Bc58"
download.file(tab2982_url, here::here("data","raw","IBGE", paste0("tab2982_mun_higher_ed_2000_downloaded_",date,".xlsx")))

tab3547_url <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela3547.xlsx&terr=NC&rank=-&query=t/3547/n6/all/v/1001643/p/all/c2/0/c1568/99713/d/v1001643%202/l/v,p%2Bc2,t%2Bc1568"
download.file(tab3547_url, here::here("data","raw","IBGE", paste0("tab3547_mun_higher_ed_2010_downloaded_",date,".xlsx")))

# Download average nominal earnings
tab2033_url_avg <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela2033.xlsx&terr=NC&rank=-&query=t/2033/n6/all/v/842/p/all/c2/6794/d/v842%202/l/v,p%2Bc2,t"
download.file(tab2033_url_avg, here::here("data","raw","IBGE", paste0("tab2033_avg_earnings_2000_2010_downloaded_",date,".xlsx")))

tab2033_url_median <- "https://sidra.ibge.gov.br/geratabela?format=xlsx&name=tabela2033.xlsx&terr=NC&rank=-&query=t/2033/n6/all/v/843/p/all/c2/6794/d/v843%202/l/v,p%2Bc2,t"
download.file(tab2033_url_median, here::here("data","raw","IBGE", paste0("tab2033_median_earnings_2000_2010_downloaded_",date,".xlsx")))
